package com.jack.payment.rabbitmq;

import com.jack.payment.constant.SettingRuleConstant;
import com.jack.payment.constant.enums.TimeEnum;
import com.jack.payment.entity.common.BusinessException;
import com.jack.payment.entity.generate.user.UserDeviceModel;
import com.jack.payment.entity.mong.RequestLogModel;
import com.jack.payment.rabbitmq.constant.RabbitmqConstant;
import com.jack.payment.rabbitmq.entity.QueueContentModel;
import com.jack.payment.rabbitmq.produer.MQSender;
import com.jack.payment.util.common.TimeUtil;
import com.jack.payment.util.redis.GenerateId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RabbitQueueSendUtil {
    private Logger log = LoggerFactory.getLogger(RabbitQueueSendUtil.class);
    @Autowired
    private MQSender mqSender;
    @Autowired
    private GenerateId generateId;

    /**
     * 支付成功后, 进入消息队列, 使用消息队列进行webhook 通知策略
     * @param orderNo
     * @param mqId
     */
    public void paymentSuccessQueueMessage(String orderNo,
                                           Long currentTime,
                                           String mqId) {
        String classPath = orderNo.getClass().getName();
        QueueContentModel queueContentModel = builderQueueContentModel(classPath, currentTime, mqId);
        queueContentModel.setSubject(2);
        queueContentModel.setData(orderNo);
        mqSender.sendMQMessage(
                RabbitmqConstant.EXCHANGE_COMMON,
                RabbitmqConstant.PAYMENT_CALLBACK,
                queueContentModel
        );
    }

    /**
     * 请求日志
     * @param requestLogModel
     * @throws BusinessException
     */
    public void requestLogQueueMessage(RequestLogModel requestLogModel) throws BusinessException {
        String classPath = requestLogModel.getClass().getName();
        QueueContentModel queueContentModel =
                builderQueueContentModel(
                        classPath,
                        requestLogModel.getCreateTime(),
                        generateId.generateMQId()
                );
        queueContentModel.setSubject(1);
        queueContentModel.setData(requestLogModel);
        mqSender.sendMQMessage(
                RabbitmqConstant.EXCHANGE_COMMON,
                RabbitmqConstant.SYSTEM_LOGGER,
                queueContentModel
        );
    }

    /**
     * 登录成功后一些耗时队列处理
     * @param userDeviceModel
     * @throws BusinessException
     */
    public void LoginSuccessQueueMessage(UserDeviceModel userDeviceModel) throws BusinessException {
        String classPath = userDeviceModel.getClass().getName();
        QueueContentModel queueContentModel =
                builderQueueContentModel(
                        classPath,
                        userDeviceModel.getLastOneTime(),
                        generateId.generateMQId()
                );
        queueContentModel.setSubject(4);
        queueContentModel.setData(userDeviceModel);
        mqSender.sendMQMessage(
                RabbitmqConstant.EXCHANGE_COMMON,
                RabbitmqConstant.LOGIN_VALUE,
                queueContentModel
        );
    }


    private String currentTableName(Long currentTime) {
        return TimeUtil.timeFormatJoin(SettingRuleConstant.QUEUE_LOG, currentTime, TimeEnum.MONTH);
    }

    /**
     * 构建一个最初的队列消息列表
     * @param classPathName
     * @param mqId
     * @return
     */
    protected QueueContentModel builderQueueContentModel(String classPathName,
                                                         Long currentTime,
                                                         String mqId) {
        return new QueueContentModel().builder()
                .dbName(currentTableName(currentTime))
                .attachClass(classPathName)
                .sendDate(currentTime)
                .isManual(0)
                .status(0)
                .count(0)
                .id(mqId)
                .build();
    }

}
